import pygame

from PyPixelCore.Setting import *
from PyPixelCore.block.Block import Block

"""
    TextBlock --> Block --> BaseBlock
    基础的文字块
    可直接使用或者继承优化
    功能:
        显示文字
        支持碰撞检测
        支持帧函数与计时器函数
        支持点击事件
        支持鼠标移入移出事件
        不支持自动换行绘制
        支持脏精灵优化
        *** 绘制会额外绘制在屏幕外从而影响效率
"""


class TextBlock(Block):
    def __init__(self, text_content="未定义文本", point_x=0, point_y=0, border_width=1, layer=0, is_alpha=False):
        super().__init__(bk_color=Color_type.WHITE, point_x=point_x, point_y=point_y, layer=layer, is_alpha=is_alpha)
        self.font = pygame.font.Font(basic_font_url, basic_font_size)
        self._font_size = basic_font_size + 1
        self._text_content = text_content
        self.font_color = Color_type.BLACK
        self.border_color = Color_type.GREY
        self.draw_func = base_draw
        self._type = Block_type.text_block
        self.border_width = border_width

        # 将字体载入图形
        self._reload_image_first()
        self.collision_rect.size = self.image.get_size()  # 后续没有进行维护

    def set_show_pos(self, x, y):
        self.rect.x = x
        self.rect.y = y

    def _reload_image_first(self):
        surf = self.font.render(self._text_content, True, self.font_color, self.bk_color)
        text_size = surf.get_size()
        if self.is_alpha:
            self.image = pygame.Surface((text_size[0] + self.border_width * 2, text_size[1] + self.border_width * 2),
                                        pygame.SRCALPHA)
        else:
            self.image = pygame.Surface((text_size[0] + self.border_width * 2, text_size[1] + self.border_width * 2))
        self.image.fill(self.bk_color)
        self.rect.size = self.image.get_size()
        # 绘制方框到主图上
        pygame.draw.rect(self.image, self.border_color, (0, 0, self.rect.width, self.rect.height),
                         width=self.border_width)
        # 绘制文字到主图上
        self.image.blit(surf, (self.border_width, self.border_width, self.rect.width, self.rect.height))

    def reload_image(self):
        if not self.is_dirty:
            return
        self.is_dirty = False
        surf = self.font.render(self._text_content, True, self.font_color, self.bk_color)
        text_size = surf.get_size()
        if self.is_alpha:
            self.image = pygame.Surface((text_size[0] + self.border_width * 2, text_size[1] + self.border_width * 2), pygame.SRCALPHA)
        else:
            self.image = pygame.Surface((text_size[0] + self.border_width * 2, text_size[1] + self.border_width * 2))
        self.rect.size = self.image.get_size()
        # 绘制方框到主图上
        if self.border_width != 0:
            pygame.draw.rect(self.image, self.border_color, (0, 0, self.rect.width, self.rect.height), width=self.border_width)
        # 绘制文字到主图上
        self.image.blit(surf, (self.border_width, self.border_width, self.rect.width, self.rect.height))

    def set_text_content(self, text_content):
        self.is_dirty = True
        self._text_content = text_content

    @property
    def text_content(self):
        return self._text_content

    def set_font(self, font_url, font_size):
        self.is_dirty = True
        self.font = pygame.font.Font(font_url, font_size)
        self._font_size = font_size + 1

    def set_color_no_None(self, font_color=None, bk_color=None, border_color=None):
        self.is_dirty = True
        if font_color is not None:
            self.font_color = font_color
        if bk_color is not None:
            self.bk_color = bk_color
        if border_color is not None:
            self.border_color = border_color

    def set_font_color(self, font_color):
        self.is_dirty = True
        self.font_color = font_color

    def set_bk_color(self, bk_color):
        self.is_dirty = True
        self.bk_color = bk_color

    def set_border_color(self, border_color):
        self.is_dirty = True
        self.border_color = border_color


def base_draw(self: TextBlock, surf):
    surf.blit(self.image, self.rect)
    if show_rect_collision:
        # pygame.draw.rect(surf, Color_type.RED, self.rect, width=3)
        pygame.draw.rect(surf, Color_type.BLACK, self.collision_rect, width=collision_surface_width)
        pygame.draw.rect(surf, Color_type.RED, self.rect, width=block_surface_width)

        # pygame.draw.circle(surf, Color_type.RED, self.rect.center, 50, width=2)
        # pygame.draw.line(surf, Color_type.RED, self.rect.bottomleft, self.rect.bottomright, width=4)
        # pygame.draw.polygon(surf, Color_type.RED, [(1, 1), (27, 90), (111, 111)], width=3)
        # pygame.draw.arc(surf, Color_type.RED, self.rect, 0, 5, width=100)
        # pygame.draw.aalines(surf, Color_type.RED, False, [(1, 1), (27, 90), (111, 111)], blend=1)

